<?php

namespace App\Service\Moment;

use App\Constants\ErrorCode;
use App\Model\Message;
use App\Model\Moment;
use App\Model\MomentLike;
use App\Model\UserInteractMsg;
use App\Service\BaseService;

/**
 * 动态点赞（取消）
 */
class MomentLikeService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $option = $this->request->input('option', 1);
        $momentId = $this->request->input('moment_id', 0);
        if (!$momentId) return $this->setError(ErrorCode::PARAMETER_ERROR);
        $moment = Moment::find($momentId);
        if (!$moment) return $this->setError(ErrorCode::PARAMETER_ERROR, '动态不存在');
        $ret = [
            'result' => MomentLike::addLike($this->getUserId(), $momentId, $option),
        ];
        //修改动态表like_num字段
        Moment::updateLikeNum($momentId, $option == 1 ? 1 : -1);

        //消息记录
        if ($option == 1) {
            UserInteractMsg::addMomentMsg($userId, $moment->user_id, $momentId, UserInteractMsg::TYPE_MOMENT_LIKE);
            Message::sendSlient($moment->user_id, 'MOMENT_NEW_MSG');
        }
        return $ret;
    }
}
